import request from "@/utils/request";
import {QueueScheduleModel} from "@/model/QueueScheduleModel";
import {PagingModel} from "@/model/PagingModel";
import {QueueItemModel} from "@/model/QueueItemModel";
import {EnvConfig} from "@/utils/index.js";
import axios from "axios";

export default class NurseClientApi {
    /**
     * 查询当天出诊的所有科目
     */
    public static getTodaySchedule(deptId: string): Promise<QueueScheduleModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/schedule/today/list`,
                method: 'GET',
                params: {
                    deptId: deptId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new QueueScheduleModel().deserialize(item)))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     * 根据设备查询关联的科目
     */
    public static getItemListByIds(deptId: string): Promise<QueueScheduleModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/item/byids/list`,
                method: 'GET',
                params: {
                    deptId: deptId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new QueueScheduleModel().deserialize(item)))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    public static getTodayEmplyeeByItem(deptId: string, itemId: string): Promise<any[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/dept/${deptId}/item/${itemId}/employee/list`,
                method: 'GET'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data)
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    public static getEmployeePatientList(deptId: string, itemId: string, state: number, source: string, searchKey: string, pageIndex: number, pageSize: number): Promise<PagingModel<QueueItemModel>> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/dept/${deptId}/item/${itemId}/patient`,
                method: 'GET',
                params: {
                    state,
                    source,
                    searchKey,
                    pageNum: pageIndex,
                    pageSize
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    let data: QueueItemModel[] = [];
                    const resultData: QueueItemModel[] = res.rows;
                    if (resultData) {
                        data = resultData.map(item => new QueueItemModel().deserialize(item));
                    }
                    res.total = +res.total
                    res.pageIndex = pageIndex
                    res.pageSize = pageSize
                    resolve(new PagingModel<QueueItemModel>().deserialize(Object.assign(res, {data})));
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     * 帮患者签到
     */
    public static signPatientByCardNo(departId: string, itemId: string, cardNo: string): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/sign/${departId}/${itemId}/${cardNo}`,
                method: 'put'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(Object.assign({}, res.data.queue, {await: res.data.await})))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     * 帮患者签到
     */
    public static signPatientByRegId(regId: string): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/sign/${regId}`,
                method: 'put'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(Object.assign({}, res.data.queue, {await: res.data.await})))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     * 取号
     */
    public static numberPatientByRegId(regId: string): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/number/${regId}`,
                method: 'put'
            }).then((res: any) => {
                if (res.code === 200) {
                    console.log('printData--------------:' + res.data);
                    axios.get(EnvConfig.printServerUrl+'?l1=科室：'+res.data.patient.departName+'&l2=姓名：'+res.data.patient.patientName+'&l3=序号：'+res.data.patient.queueNo+'&l4=卡号：'+res.data.processingCardNo+'&l5=请等待叫号！过号重签！')
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     * 过号
     */
    public static passedPatientByRegId(regId: string): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/passed/${regId}`,
                method: 'put'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(Object.assign({}, res.data.queue, {await: res.data.await})))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     * 就诊完成
     */
    public static completePatientByRegId(regId: string): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/complete/${regId}`,
                method: 'put'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(Object.assign({}, res.data.queue, {await: res.data.await})))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     * 重呼
     */
    public static AgainCallByRegId(regId:string): Promise < QueueItemModel > {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/again/call/${regId}`,
                method: 'put'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(Object.assign({}, res.data.queue, {await: res.data.await})))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     *  指派医生
     * @param doctorId
     * @param itemId
     * @param deptId
     * @param categoryId
     * @param targetDoctorId
     * @param queueHisId
     * @param subQueueFlag
     * @param priorityType
     */

    public static changeQueueItemSignDoctor(doctorId:string, itemId:string, deptId:string, categoryId:string, targetDoctorId:string, queueHisId:string, subQueueFlag:number, priorityType:number): Promise < QueueItemModel > {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/set/doctor/sign`,
                method: 'put',
                params: {
                    deptId,
                    categoryId,
                    itemId,
                    targetDoctorId,
                    queueHisId,
                    subQueueFlag,
                    priorityType
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(Object.assign({}, res.data.queue, {await: res.data.await})))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     *  设置优先级
     * @param doctorId
     * @param itemId
     * @param deptId
     * @param categoryId
     * @param queueHisId
     * @param subQueueFlag
     * @param priorityType
     */

    public static setPatientToPriority(doctorId:string, categoryId:string, itemId:string, deptId:string, queueHisId:string, subQueueFlag:number, priorityType:number): Promise < QueueItemModel > {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/set/patient/priority`,
                method: 'put',
                params: {
                    deptId,
                    itemId,
                    queueHisId,
                    doctorId,
                    categoryId,
                    subQueueFlag,
                    priorityType
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(Object.assign({}, res.data.queue, {await: res.data.await})))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    /**
     * 取消就诊
     */
    public static clearPatientEmployeeByRegId(queueHisId: string, itemId: string, deptId: string, categoryId: string, subQueueFlag: number): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/nurse/clear/doctor`,
                method: 'put',
                params: {
                    deptId,
                    categoryId,
                    itemId,
                    queueHisId,
                    subQueueFlag
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(Object.assign({}, res.data.queue, {await: res.data.await})))
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

        // 查询读卡器
    public static readCardNo(): Promise < String > {
        return new Promise((resolve, reject) => {
            request({
                url: EnvConfig.huaxuServerApi,
                method: 'GET'
            }).then((res: any) => {
                console.log('===========', res)
            }, () => {
                reject()
            })
        })
    }

}